﻿using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace nMongo
{
    public static class MongoCollectionExtensions
    {
        public static nMongo.MongoCollectionIdentity GetObjectIdentity<T>(this MongoDB.Driver.MongoCollection<T> collection, T oneObject)
        {
            nMongo.MongoCollectionIdentity returned = new nMongo.MongoCollectionIdentity();
            PropertyInfo property = typeof(T)
                .GetProperties()
                .FirstOrDefault(
                    x => x.CustomAttributes.Any(
                        y => y.AttributeType == typeof(MongoDB.Bson.Serialization.Attributes.BsonIdAttribute)));
            if (property != null)
            {
                returned.Value = property.GetValue(oneObject);
                returned.Type = property.PropertyType;
            }
            return returned;
        }

        public static IQueryable<T> Queryable<T>(this MongoDB.Driver.MongoCollection<T> collection) 
            where T : class
        {
            return collection.AsQueryable();
        }
    }
}

namespace MongoDB.Driver
{
    public static class MongoCollectionExtensions
    {
        public static nMongo.MongoCollectionIdentity GetObjectIdentity<T>(this MongoDB.Driver.MongoCollection<T> collection, T oneObject)
        {
            return nMongo.MongoCollectionExtensions.GetObjectIdentity<T>(collection, oneObject);
        }

        public static IQueryable<T> Queryable<T>(this MongoDB.Driver.MongoCollection<T> collection)
            where T : class
        {
            return nMongo.MongoCollectionExtensions.Queryable<T>(collection);
        }
    }
}
